<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="调入确认">
              <j-dict-select-tag type="list" v-model="queryParam.confirmFlag" dictCode="yn" placeholder="请选择调入确认" />
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="单据日期">
              <j-date placeholder="请选择单据日期" v-model="queryParam.billDate"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="调出库房">
              <j-search-select-tag placeholder="请选择调出库房" v-model="queryParam.warehouseOut" dict="mdm_warehouse,warehouse_name,id"/>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :xl="6" :lg="7" :md="8" :sm="24">
              <a-form-item label="调入库房">
                <j-search-select-tag placeholder="请选择调入库房" v-model="queryParam.warehouseIn" dict="mdm_warehouse,warehouse_name,id"/>
              </a-form-item>
            </a-col>
          </template>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('库存调拨')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
<!--      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>-->
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchConfirm"><a-icon type="import"/>调入</a-menu-item>
          <a-menu-item key="2" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <div  style="float: right">
          <a-checkbox style="color: #1890FF;margin-right: 12px" :checked="sameCheck" @change="sameCheckChang">同单据批量选中</a-checkbox>
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row>
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!='rowIndex'&& item.dataIndex!='action'">
                      <a-col :span="6"><a-checkbox :value="item.dataIndex">{{ item.title }}</a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />自定义列</a>
          </a-popover>
        </div>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{x:true}"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onSelect: onSelect, onSelectAll:onSelectAll}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>
        <template slot="materialNo" slot-scope="text, record">
          <j-ellipsis :value="text" :length="12"/>
        </template>
        <template slot="materialDesc" slot-scope="text, record">
          <j-ellipsis :value="text" :length="12"/>
        </template>
        <template slot="remark" slot-scope="text, record">
          <j-ellipsis :value="text" :length="12"/>
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <span v-if="record.confirmFlag === '1'" style="color: #C0C0C0">调入</span>
          <a v-else @click="handleConfirm(record)">调入</a>
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if="record.confirmFlag !== '1'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <mm-stock-transfer-modal ref="modalForm" @ok="modalFormOk"/>
    <mm-stock-transfer-detail-confirm-modal ref="detailConfirmTable" @ok="confirmTableOk"/>

  </a-card>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import MmStockTransferModal from './modules/MmStockTransferModal'
  import MmStockTransferDetailConfirmModal from './modules/MmStockTransferDetailConfirmModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import '@/assets/less/TableExpand.less'
  import {deleteAction} from "@/api/manage";

  export default {
    name: "MmStockTransferList",
    mixins:[JeecgListMixin],
    components: {
      MmStockTransferModal,
      MmStockTransferDetailConfirmModal,
    },
    data () {
      return {
        description: '库存调拨管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'单据号',
            align:"center",
            dataIndex: 'billNo'
          },
          {
            title:'行号',
            align:"center",
            dataIndex: 'lineNo'
          },
          {
            title:'物料编号',
            align:"center",
            dataIndex: 'materialNo',
            scopedSlots: { customRender: 'materialNo' },
          },
          {
            title:'物料描述',
            align:"center",
            dataIndex: 'materialDesc',
            scopedSlots: { customRender: 'materialDesc' },
          },
          {
            title: '单位',
            align:"center",
            dataIndex: 'unit_dictText',
          },
          {
            title: '制单数量',
            align:"center",
            dataIndex: 'billQty',
          },
          {
            title: '调拨数量',
            align:"center",
            dataIndex: 'transferQty',
          },
          {
            title:'单据日期',
            align:"center",
            dataIndex: 'billDate',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'调入确认',
            align:"center",
            dataIndex: 'confirmFlag',
            customRender: (text) => (text ? filterMultiDictText(this.dictOptions['confirmFlag'], text) : ''),
          },
          {
            title:'确认人',
            align:"center",
            dataIndex: 'confirmEmp_dictText'
          },
          {
            title:'确认时间',
            align:"center",
            dataIndex: 'confirmDate',
            customRender:function (text) {
              return !text?"":(text.length>19?text.substr(0,19):text)
            }
          },
          {
            title:'调出库房',
            align:"center",
            dataIndex: 'warehouseOut_dictText'
          },
          {
            title:'调出库位',
            align:"center",
            dataIndex: 'storageLocationOut_dictText'
          },
          {
            title:'调出批次',
            align:"center",
            dataIndex: 'batchOut'
          },
          {
            title:'调入库房',
            align:"center",
            dataIndex: 'warehouseIn_dictText'
          },
          {
            title:'调入库位',
            align:"center",
            dataIndex: 'storageLocationIn_dictText'
          },
          {
            title:'调入批次',
            align:"center",
            dataIndex: 'batchIn'
          },
          {
            title:'调出库管员',
            align:"center",
            dataIndex: 'warehouseEmpOut_dictText'
          },
          {
            title:'调入库管员',
            align:"center",
            dataIndex: 'warehouseEmpIn_dictText'
          },
          {
            title: '单价',
            align:"center",
            dataIndex: 'unitPrice',
          },
          {
            title: '金额',
            align:"center",
            dataIndex: 'sumPrice',
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark',
            scopedSlots: { customRender: 'remark' },
          },
          {
            title:'创建人',
            align:"center",
            dataIndex: 'createBy_dictText'
          },
          {
            title:'创建时间',
            align:"center",
            dataIndex: 'createTime',
            customRender:function (text) {
              return !text?"":(text.length>10?text.substr(0,10):text)
            }
          },
          {
            title:'所属部门',
            align:"center",
            dataIndex: 'sysOrgCode_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/mm/stockTransfer/detailPageList",
          detailList: "/mm/stockTransfer/detailList",
          delete: "/mm/stockTransfer/deleteDetail",
          deleteBatch: "/mm/stockTransfer/deleteDetailBatch",
          exportXlsUrl: "/mm/stockTransfer/exportXls",
          importExcelUrl: "mm/stockTransfer/importExcel",
          
        },
        dictOptions:{},
        superFieldList:[],
        sameCheck: false,
      }
    },
    created() {
      this.$set(this.dictOptions, 'confirmFlag', [{text:'是',value:'1'},{text:'否',value:'0'}])
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
         fieldList.push({type:'string',value:'billNo',text:'单据号',dictCode:''})
         fieldList.push({type:'date',value:'billDate',text:'单据日期'})
         fieldList.push({type:'sel_search',value:'warehouseOut',text:'调出库房',dictTable:'mdm_warehouse', dictText:'warehouse_name', dictCode:'id'})
         fieldList.push({type:'sel_search',value:'warehouseIn',text:'调入库房',dictTable:'mdm_warehouse', dictText:'warehouse_name', dictCode:'id'})
         fieldList.push({type:'sel_user',value:'warehouseEmpOut',text:'调出库管员'})
         fieldList.push({type:'sel_user',value:'warehouseEmpIn',text:'调入库管员'})
         fieldList.push({type:'string',value:'status',text:'状态',dictCode:'mm_stock_transfer_status'})
         fieldList.push({type:'string',value:'remark',text:'备注',dictCode:''})
         fieldList.push({type:'string',value:'createBy',text:'创建人',dictCode:''})
         fieldList.push({type:'date',value:'createTime',text:'创建时间'})
         fieldList.push({type:'string',value:'sysOrgCode',text:'所属部门',dictCode:''})
        this.superFieldList = fieldList
      },
      sameCheckChang(e){
        this.sameCheck = e.target.checked;
      },
      batchDel: function () {
        if(!this.url.deleteBatch){
          this.$message.error("请设置url.deleteBatch属性!")
          return
        }
        if (this.selectedRowKeys.length <= 0) {
          this.$message.warning('请选择一条记录！');
          return;
        } else {
          var ids = "";
          for (var a = 0; a < this.selectedRowKeys.length; a++) {
            ids += this.selectedRowKeys[a] + ",";
          }
          var that = this;
          this.$confirm({
            title: "确认删除",
            content: "是否删除选中数据?",
            onOk: function () {
              that.loading = true;
              deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
                if (res.success) {
                  //重新计算分页问题
                  that.reCalculatePage(that.selectedRowKeys.length)
                  that.$message.success(res.message);
                  that.loadData();
                  that.onClearSelected();
                } else {
                  that.$message.warning(res.message);
                }
              }).finally(() => {
                that.loading = false;
              });
            }
          });
        }
      },
      handleConfirm(record){
        this.$refs.detailConfirmTable.edit([record]);
        this.$refs.detailConfirmTable.disableSubmit = false;
      },
      batchConfirm(){
        let hasConfirm = this.selectionRows.some(row => row.confirmFlag === '1')
        if(hasConfirm){
          this.$message.error("请选择未调入记录")
          return;
        }
        this.$refs.detailConfirmTable.edit(this.selectionRows);
        this.$refs.detailConfirmTable.disableSubmit = false;
      },
      confirmTableOk() {
        // 新增/修改 成功时，重载列表
        this.loadData();
        //清空列表选中
        this.onClearSelected()
      },
      handleEdit: function (record) {
        this.$refs.modalForm.edit(record);
        this.$refs.modalForm.title = "编辑";
        this.$refs.modalForm.disableSubmit = record.status !== '01';
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>